package com.jxs.android.ges;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.ges.data.WorkLogInfo;
import com.jxs.android.soap.SoapUtil;
import com.jxs.android.util.OpTimer;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.Bu_Work_Log_Item;
import com.jxs.android.weiget.TopBar;

public class Work_Log_Page extends LoadingMoreAbstractList {

	private final int current_PageSize = 15;

	private String manager_id = "";// 客户经理ID
	private OpTimer opTimer = new OpTimer(1);
	private String start_date = opTimer.getStartTime();// 开始日期(格式：yyyymmdd,例如：20120313)（多条件查询，可空）
	private String end_date = opTimer.getEndTime();// 结束日期(格式：yyyymmdd,例如：20120313)（多条件查询，可

	private WorkLogInfoAdapter adapter;

	private TopBar topBar;
	private boolean isPaused = false;

	@Override
	public void loadingData(int currentPage, boolean showDialog) {
		PAGE_SIZE = current_PageSize;
		getWebServiceData(GesApplication.getUserInfo().getTicket(), Which.WORK_LOG_PAGE, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		}, manager_id, start_date, end_date, PAGE_SIZE, currentPage);
	}

	@Override
	public int getLayout() {
		return R.layout.bu_common_with_list;
	}

	@Override
	protected void extendsLayout() {
		super.extendsLayout();
		initExtends((LinearLayout) findViewById(R.id.bu_common_extends));
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		this.topBar = topBar;
		this.topBarReturn(topBar);
		this.topBar.setTitle(R.string.home_work_note);
		this.topBar.setFilterBtnText(R.string.prompt_filter);
		this.topBar.hindSpinnerBtn();
		this.topBar.setFilterBtnVisibility(View.VISIBLE);

		topBar.showLogicBtn();
		topBar.setLogicBtnText("新建");

		topBar.setBtnListern(new TopBar.BtnListern() {

			@Override
			@Deprecated
			public void onSpinnerBtn() {
			}

			@Override
			public void onLogicBtn() {
				Intent intent = new Intent(Work_Log_Page.this, WorkLogNewCreate.class);
				startActivityForResult(intent, NEW_CREATE_REQ);
			}

			@Override
			public void onFilterBtn() {
				Intent intent = new Intent(Work_Log_Page.this, Bu_Filter_Page.class);
				Bundle bundle = new Bundle();
				bundle.putString("startTime", start_date);
				bundle.putString("endTime", end_date);
				bundle.putString("cst_id", "");
				bundle.putString("manager_id", manager_id);
				bundle.putString("hide", "cst");
				intent.putExtras(bundle);
				startActivityForResult(intent, FILTER_REQ);
			}
		});

	}

	final int NEW_CREATE_REQ = 0xa001;
	final int FILTER_REQ = 0xa002;
	final int REQ_DETAIL = 0x0b01;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (NEW_CREATE_REQ == requestCode && resultCode == RESULT_OK) {// 新增后刷新第一页
			currentPage = 1;
			loadingData(currentPage, true);
		} else if (FILTER_REQ == requestCode && resultCode == RESULT_OK) {// 过滤后按照条件刷新
			Bundle bundle = data.getExtras();
			manager_id = bundle.getString("manager_id");
			start_date = bundle.getString("startTime");
			end_date = bundle.getString("endTime");
			currentPage = 1;
			loadingData(currentPage, true);
		} else if (REQ_DETAIL == requestCode && resultCode == RESULT_OK) {
			if (null != data && null != data.getExtras()) {
				Bundle bundle = data.getExtras();
				if (bundle.containsKey("delete") && bundle.getBoolean("delete")) {// 删除后移除该项
					adapter.removeWorkLogInfo(bundle.getInt("position"));
				} else if (bundle.containsKey("edit") && bundle.getBoolean("edit")) {// 修改后刷新本项
					adapter.getItem(bundle.getInt("position")).today_content = bundle.getString("content");
					adapter.notifyDataSetChanged();
				}
			}
		}
	}

	private void initExtends(LinearLayout bu_common_extends) {
		Bu_Work_Log_Item item = new Bu_Work_Log_Item(this);
		item.setValue("序号", "日期", "客户\n经理", "内容");
		item.setBackGroud(R.drawable.row0);
		item.setTextColor(getResources().getColor(R.color.white));
		item.setContentGravityCenter();
		// bu_common_extends.addView(item);
	}

	private void toDetail(WorkLogInfo custInfo, int postion) {
		Intent intent = new Intent(this, WorkLogDetail.class);
		Bundle bundle = new Bundle();
		bundle.putString("wl_id", custInfo.wl_id);
		bundle.putInt("postion", postion);
		intent.putExtras(bundle);
		startActivityForResult(intent, REQ_DETAIL);
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new WorkLogInfoAdapter();
		return adapter;
	}

	@Override
	public List<WorkLogInfo> paserData(SoapObject retMsgObject, Object... params) {
		List<WorkLogInfo> noticeListInfos = new ArrayList<WorkLogInfo>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				WorkLogInfo workLogInfo = new WorkLogInfo();
				workLogInfo.wl_id = SoapUtil.getValue(taskBeanObject, "wl_id");
				workLogInfo.create_date = SoapUtil.getValue(taskBeanObject, "create_date");
				workLogInfo.staff_name = SoapUtil.getValue(taskBeanObject, "staff_name");
				workLogInfo.today_content = SoapUtil.getValue(taskBeanObject, "today_content");
				workLogInfo.week_desc = SoapUtil.getValue(taskBeanObject, "week_desc");

				noticeListInfos.add(workLogInfo);
			}
		}
		return noticeListInfos;
	}

	@Override
	public void updateList(Message msg) {
		@SuppressWarnings("unchecked")
		List<WorkLogInfo> tems = ((List<WorkLogInfo>) msg.obj);
		refreshListView.onRefreshComplete();
		empytText.setVisibility(View.GONE);
		if (currentPage == 1) {
			if (tems.size() < 1) {
				empytText.setVisibility(View.VISIBLE);
				loadMoreButton.setText("数据已全部获取完毕");
				loadMoreButton.setEnabled(false);
			}
		}
		adapter.addWorkLogInfos(tems);
		adapter.notifyDataSetChanged();

		if (tems.size() < PAGE_SIZE) {
			loadMoreButton.setText("数据已全部获取完毕");
			loadMoreButton.setEnabled(false);
		} else {
			loadMoreButton.setText("查看更多...");
			loadMoreButton.setEnabled(true);
			currentPage++;
		}
	}

	private class WorkLogInfoAdapter extends BaseAdapter {

		private List<WorkLogInfo> workLogInfos = new ArrayList<WorkLogInfo>();

		private ViewHolder holder = null;

		public void addWorkLogInfos(List<WorkLogInfo> custInfos) {
			if (currentPage == 1) {
				this.workLogInfos.clear();
			}
			this.workLogInfos.addAll(custInfos);
		}

		public void removeWorkLogInfo(int position) {
			workLogInfos.remove(position);
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return workLogInfos.size();
		}

		@Override
		public WorkLogInfo getItem(int position) {
			return workLogInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final WorkLogInfo custInfo = workLogInfos.get(position);

			if (convertView == null) {
				convertView = LayoutInflater.from(Work_Log_Page.this).inflate(R.layout.bu_list_item_work_note, null);
				holder = new ViewHolder();
				holder.index = (TextView) convertView.findViewById(R.id.index);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.person = (TextView) convertView.findViewById(R.id.person);
				holder.content = (TextView) convertView.findViewById(R.id.content);
				holder.item_arrow = (ImageView) convertView.findViewById(R.id.item_arrow);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.index.setText("No." + String.valueOf(position + 1));
			holder.date.setText(Html.fromHtml("<font color='#990000'><b>" + custInfo.create_date + "</b></font> " + custInfo.week_desc));
			holder.person.setText(custInfo.staff_name);

			String customHtml = custInfo.today_content.replace("\\n", "<br>");
			holder.content.setText(Html.fromHtml(customHtml));
			holder.item_arrow.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					toDetail(custInfo, position);
				}
			});
			return convertView;
		}

		class ViewHolder {
			TextView index, date, person, content;
			ImageView item_arrow;
		}

	}

	@Override
	public String getMethod() {
		// 2.4.2 getWorkLogPage 【工作日志分页查询】
		return "getWorkLogPage";
	}

	@Override
	protected void onPause() {
		isPaused = true;
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (isPaused) {//

		}
		super.onResume();
		isPaused = false;
	}

}